/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


package com.newgonzo.cannonball.css
{
	import com.newgonzo.cannonball.w3c.dom.css.ICSSStyleSheet;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSStyleDeclaration;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSRule;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSStyleRule;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSValue;
	
	import com.newgonzo.cannonball.w3c.css.sac.IDocumentHandler;
	import com.newgonzo.cannonball.w3c.css.sac.ISelectorList;
	import com.newgonzo.cannonball.w3c.css.sac.ILexicalUnit;
	import com.newgonzo.cannonball.w3c.css.sac.ISACMediaList;
	
	public class CSSDocumentHandler implements IDocumentHandler
	{
		private var document:CSSDocument;
		private var currentSheet:ICSSStyleSheet;
		private var currentRule:ICSSRule;
		private var currentStyle:ICSSStyleDeclaration;
		private var currentProvider:CSSValueProvider
		
		public function CSSDocumentHandler()
		{	
			
		}
		
		public function set currentDocument(value:CSSDocument):void
		{
			document = value;
		}
		
		public function set currentStyleSheet(value:ICSSStyleSheet):void
		{
			currentSheet = value;
		}
		
		public function set currentValueProvider(value:CSSValueProvider):void
		{
			currentProvider = value;	
		}
		
		public function startDocument(source:String):void
		{
			if(!currentSheet)
			{
				currentSheet = document.createStyleSheet();
			}
		}
		
		public function startSelector(selectors:ISelectorList):void
		{
			currentProvider = document.createValueProvider();
			
			var style:ICSSStyleDeclaration = document.createStyleDeclaration(currentProvider, currentRule);
			
			currentRule = document.createStyleRule(selectors, style);
			currentSheet.appendRule(currentRule);
		}
		
		public function endSelector(selectors:ISelectorList):void
		{
			
		}
		
		public function importStyle(uri:String):void
		{
			throw new Error("NOT SUPPORTED YET");
		}
		
		public function startMedia(media:ISACMediaList):void
		{
			throw new Error("NOT SUPPORTED YET");
		}
		
		public function endMedia(media:ISACMediaList):void
		{
			throw new Error("NOT SUPPORTED YET");
		}
		
		public function ignorableAtRule(atRule:String):void
		{
			throw new Error("NOT SUPPORTED YET");
		}
		
		//public function ignorableAtRule(atRule:String):void
		//public function namespaceDeclaration(prefix:String, uri:String):void 
		//public function startMedia(media:IMediaList):void
		//public function endMedia(media:IMediaList):void
		//public function startPage(name:String, pseudoPage:String):void
		//public function endPage(name:String, pseudoPage:String):void
		//public function startFontFace():void
		//public function endFontFace():void
		public function property(name:String, value:ILexicalUnit, important:Boolean):void
		{
			var cssValue:ICSSValue = document.createValue(value);
			var priority:String = important ? CSSDocument.PRIORITY_IMPORTANT : "";
			
			currentProvider.setProperty(name, cssValue, priority);
		}
		
		public function endDocument(source:String):void
		{
			currentSheet = null;
			currentRule = null;
			currentProvider = null;
		}
		
		public function comment(text:String):void
		{
		}
	}
}
